package com.login.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.login.bean.Buyer;
import com.login.bean.Saler;
import com.login.dao.ISalerDao;
import com.login.utils.DbUtils;

public class SalerDaoImpl implements ISalerDao {

	@Override
	public boolean SalerLogin(String username, String password) {
		String sql="select * from tb_seller where name=? and  passWord=?";		
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);		
		try {		
			ps.setString(1, username);
			ps.setString(2, password);
			ResultSet rs=ps.executeQuery();				
			while(rs.next()){		
			 if(rs.getString("name").equals(username)&&rs.getString("passWord").equals(password))
				{
					return true;				
				}
			 return false;
			} 
			}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DbUtils.close();	
		
		return false;
	}

	@Override
	public int saveSeller(Buyer user, String time) {
		int result = 0 ;
		String sql = "insert into tb_seller (name,passWord,identify,phoneNumber,registTime) values(?,?,?,?,?)";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		try {
			ps.setString(1, user.getName());
			ps.setString(2, user.getPwd());
			ps.setString(3, user.getIdentify());
			ps.setString(4, user.getPhoneNumber());
			ps.setString(5, time);
			 result=ps.executeUpdate();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return result;
	}
	@Override
	public int deleteSaler(String username) {
		String sql="delete from tb_seller where name=?";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		int result=0;
		try {
			ps.setString(1, username);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
		
	}

	@Override
	public int updateSaler(String userpwd, String name) {
		String sql="update tb_seller set passWord=? where name=?";
		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
		int rs=0;
		try {
			ps.setString(1, userpwd);
			ps.setString(2, name);			
			rs=ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		return rs;
	}	

	@Override
	public int saveSalerInformation(String address, String contactNumber,
			String realName, Buyer user) {
		int result=0;
//		String sql="update tb_seller set address=?,contactNumber=?,realName=? where name=?";
//		PreparedStatement ps=DbUtils.executePrepareStetement(sql);
//		try {
//			ps.setString(1, address);
//			ps.setString(2, contactNumber);
//			ps.setString(3, realName);	
//			ps.setString(4, user.getName());
//			result=ps.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return result;
	}

	@Override
	public List<Saler> findByUsername(String username) {
		String sql="select * from tb_seller where name like '%"+username+"%'" ;
		ResultSet rs=DbUtils.executeQuery(sql);
		List<Saler> list=new ArrayList<Saler>();
		try {	
			if(rs.next()){
				Saler user=new Saler();
				user.setIdentify(rs.getString("identify"));
				user.setName(rs.getString("name"));
				user.setPhoneNumber(rs.getString("phoneNumber"));
				user.setPwd(rs.getString("passWord"));
				user.setAddress(rs.getString("address"));
				user.setContactNumber(rs.getString("contactNumber"));			
				list.add(user);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return list;		
	}	

	@Override
	public List<Saler> findAllSeller() {
		String sql="select * from tb_seller";				
		List<Saler> list =new ArrayList<Saler>();
		ResultSet rs=DbUtils.executeQuery(sql);
		try {
			while(rs.next()){
				Saler user = new Saler();
				user.setName(rs.getString("name"));
				user.setPwd(rs.getString("password"));
				user.setIdentify(rs.getString("identify"));
				user.setPhoneNumber(rs.getString("phoneNumber"));
				list.add(user);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return list;	
	}
//
//	@Override
//	public List<User> findType(String userType) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override


}
